package com.example.hissys.controller;

import com.example.hissys.entity.Doctor;
import com.example.hissys.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

@RestController
@RequestMapping("/api/doctors")
@CrossOrigin(origins = "*")
@Slf4j
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    /**
     * 根据部门ID获取医生列表
     * @param departmentId 部门ID
     * @return 医生列表
     */
    @GetMapping("/department/{departmentId}")
    public ResponseEntity<?> getDoctorsByDepartmentId(@PathVariable Integer departmentId) {
        try {
            List<Doctor> doctors = doctorService.getDoctorsByDepartmentId(departmentId);
            if (doctors.isEmpty()) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(doctors);
        } catch (Exception e) {
            log.error("Error fetching doctors for department ID: {}", departmentId, e);
            return ResponseEntity.badRequest().body("获取医生列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取医生详情
     * @param id 医生ID
     * @return 医生详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getDoctorById(@PathVariable Integer id) {
        try {
            Doctor doctor = doctorService.getDoctorById(id);
            if (doctor == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(doctor);
        } catch (Exception e) {
            log.error("Error fetching doctor with ID: {}", id, e);
            return ResponseEntity.badRequest().body("获取医生详情失败：" + e.getMessage());
        }
    }
} 